// pages/edit/edit.js
const {detail, option, update, add, list, follow, faceMatch, session, rubbish, levelOption, baiduIdentify} = require("../../api/index.js")
const des = require('../../utils/des.js')
const utils = require("../../utils/index.js")
Page({

/**
* 页面的初始数据
*/data: {
imgIcon: "../../static/upload.png",
editStatus: false,
baseURL:'',
sessionReadArr:[],
detailList: null,
id: "",
cross:"",
ruleForm:{},
userid:getApp().globalData.userInfo.id,
userInfo:getApp().globalData.userInfo,
ro:{
},
xuehao:"",
mima:"",
xueshengxingming:"",
xingbieList:"男,女".split(','),
xingbieIndex:0,
xueyuan:"",
zhuanye:"",
nianji:"",
xueliList:"小学,初中,高中,中专,大专,本科,研究生,博士".split(','),
xueliIndex:0,
shifoubiyeList:"是,否".split(','),
shifoubiyeIndex:0,
biyenianfen:"",
xiangmufuzerenList:"是,否".split(','),
xiangmufuzerenIndex:0,
suozaixuexiao:"",
youxiang:"",
touxiang:"",
chuangyelingyu:"",
canjiahuodong:"",
canyuxiangmu:"",
leixingList:"在校生,毕业生".split(','),
leixingIndex:0,
chengshiIndex: 0,
chengshiList: [],
yuanquIndex: 0,
yuanquList: [],
jiedaoIndex: 0,
jiedaoList: [],
email:"",
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(options) {
let userid
if (options?.id) {
this.setData({
editStatus: true
})
}
let nowTable = wx.getStorageSync("nowTable");
const res = await session(nowTable)
if(res.data.code==0){
getApp().globalData.userInfo=res?.data
userid = res?.data.id
this.setData({
userid
})
}
let baseURL =wx.getStorageSync('baseURL') + '/'
const id = getApp().globalData.detailId
this.setData({
refid:id,
baseURL
})
//人脸识别
 let xingbieList="男,女".split(',');
 let xingbieIndex=0;
 this.setData({
   xingbieList,
   xingbieIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
 let xueliList="小学,初中,高中,中专,大专,本科,研究生,博士".split(',');
 let xueliIndex=0;
 this.setData({
   xueliList,
   xueliIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
 let shifoubiyeList="是,否".split(',');
 let shifoubiyeIndex=0;
 this.setData({
   shifoubiyeList,
   shifoubiyeIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
 let xiangmufuzerenList="是,否".split(',');
 let xiangmufuzerenIndex=0;
 this.setData({
   xiangmufuzerenList,
   xiangmufuzerenIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
 let leixingList="在校生,毕业生".split(',');
 let leixingIndex=0;
 this.setData({
   leixingList,
   leixingIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
const chengshires = await option('chengshi/chengshi')
chengshires.data.unshift('请选择城市')
this.setData({
chengshiList: chengshires.data
})
const yuanqures = await option('yuanqu/yuanqu')
yuanqures.data.unshift('请选择园区')
this.setData({
yuanquList: yuanqures.data
})
const jiedaores = await option('jiedao/jiedao')
jiedaores.data.unshift('请选择街道')
this.setData({
jiedaoList: jiedaores.data
})
let  ro=this.data.ro;
if(options?.cross){
var obj = wx.getStorageSync('crossObj');
let refobjempty={}
for (var o in obj){
if(o=='xuehao'){
   refobjempty["xuehao"]=obj[o]
ro.xuehao = true;
continue;
}else{
}
if(o=='mima'){
   refobjempty["mima"]=obj[o]
ro.mima = true;
continue;
}else{
}
if(o=='xueshengxingming'){
   refobjempty["xueshengxingming"]=obj[o]
ro.xueshengxingming = true;
continue;
}else{
}
if(o=='xingbie'){
   refobjempty["xingbie"]=obj[o]
ro.xingbie = true;
continue;
}else{
}
if(o=='xueyuan'){
   refobjempty["xueyuan"]=obj[o]
ro.xueyuan = true;
continue;
}else{
}
if(o=='zhuanye'){
   refobjempty["zhuanye"]=obj[o]
ro.zhuanye = true;
continue;
}else{
}
if(o=='nianji'){
   refobjempty["nianji"]=obj[o]
ro.nianji = true;
continue;
}else{
}
if(o=='xueli'){
   refobjempty["xueli"]=obj[o]
ro.xueli = true;
continue;
}else{
}
if(o=='shifoubiye'){
   refobjempty["shifoubiye"]=obj[o]
ro.shifoubiye = true;
continue;
}else{
}
if(o=='biyenianfen'){
   refobjempty["biyenianfen"]=obj[o]
ro.biyenianfen = true;
continue;
}else{
}
if(o=='xiangmufuzeren'){
   refobjempty["xiangmufuzeren"]=obj[o]
ro.xiangmufuzeren = true;
continue;
}else{
}
if(o=='suozaixuexiao'){
   refobjempty["suozaixuexiao"]=obj[o]
ro.suozaixuexiao = true;
continue;
}else{
}
if(o=='youxiang'){
   refobjempty["youxiang"]=obj[o]
ro.youxiang = true;
continue;
}else{
}
if(o=='touxiang'){
refobjempty['touxiang']=obj[o];
refobjempty['touxiangPath']=obj[o].split(",")[0];
ro.touxiang = true;
continue;
}else{
}
if(o=='chuangyelingyu'){
   refobjempty["chuangyelingyu"]=obj[o]
ro.chuangyelingyu = true;
continue;
}else{
}
if(o=='canjiahuodong'){
   refobjempty["canjiahuodong"]=obj[o]
ro.canjiahuodong = true;
continue;
}else{
}
if(o=='canyuxiangmu'){
   refobjempty["canyuxiangmu"]=obj[o]
ro.canyuxiangmu = true;
continue;
}else{
}
if(o=='leixing'){
   refobjempty["leixing"]=obj[o]
ro.leixing = true;
continue;
}else{
}
if(o=='chengshi'){
   refobjempty["chengshi"]=obj[o]
ro.chengshi = true;
continue;
}else{
}
if(o=='yuanqu'){
   refobjempty["yuanqu"]=obj[o]
ro.yuanqu = true;
continue;
}else{
}
if(o=='jiedao'){
   refobjempty["jiedao"]=obj[o]
ro.jiedao = true;
continue;
}else{
}
if(o=='email'){
   refobjempty["email"]=obj[o]
ro.email = true;
continue;
}else{
}
}
let  statusColumnName=wx.getStorageSync('statusColumnName');
statusColumnName=statusColumnName.replace('[',"").replace(']',"");
this.setData({
ro,
cross:options?.cross,
statusColumnName
})
this.setData(refobjempty)
}
if(id){
// 如果上一级页面传递了id，获取改id数据信息
const data=getApp().globalData.detailList;
let dataEmpty={};
const  def_4= 0  ;
this.data.xingbieList.map((v, index) => {
if (v == data.xingbie|| (v == def_4 && def_4 !=null)) {
 this.setData({
   xingbieIndex: index,
   xingbie: v,
 })
}else if (this.data.xingbieList.length == 1) {
 this.setData({
   xingbieIndex: 0,
   xingbie: v,
 })
}
})
const  def_8= 0  ;
this.data.xueliList.map((v, index) => {
if (v == data.xueli|| (v == def_8 && def_8 !=null)) {
 this.setData({
   xueliIndex: index,
   xueli: v,
 })
}else if (this.data.xueliList.length == 1) {
 this.setData({
   xueliIndex: 0,
   xueli: v,
 })
}
})
const  def_9= 0  ;
this.data.shifoubiyeList.map((v, index) => {
if (v == data.shifoubiye|| (v == def_9 && def_9 !=null)) {
 this.setData({
   shifoubiyeIndex: index,
   shifoubiye: v,
 })
}else if (this.data.shifoubiyeList.length == 1) {
 this.setData({
   shifoubiyeIndex: 0,
   shifoubiye: v,
 })
}
})
const  def_11= 0  ;
this.data.xiangmufuzerenList.map((v, index) => {
if (v == data.xiangmufuzeren|| (v == def_11 && def_11 !=null)) {
 this.setData({
   xiangmufuzerenIndex: index,
   xiangmufuzeren: v,
 })
}else if (this.data.xiangmufuzerenList.length == 1) {
 this.setData({
   xiangmufuzerenIndex: 0,
   xiangmufuzeren: v,
 })
}
})
const  def_18= 0  ;
this.data.leixingList.map((v, index) => {
if (v == data.leixing|| (v == def_18 && def_18 !=null)) {
 this.setData({
   leixingIndex: index,
   leixing: v,
 })
}else if (this.data.leixingList.length == 1) {
 this.setData({
   leixingIndex: 0,
   leixing: v,
 })
}
})
chengshires.data.map((item, index) => {
if (item == data.chengshi) {
 this.setData({
chengshiIndex: index,
chengshi: item
 })
}else if (this.data.chengshiList.length == 1) {
this.setData({
chengshiIndex: 0,
chengshi: v
 })
}
})
yuanqures.data.map((item, index) => {
if (item == data.yuanqu) {
 this.setData({
yuanquIndex: index,
yuanqu: item
 })
}else if (this.data.yuanquList.length == 1) {
this.setData({
yuanquIndex: 0,
yuanqu: v
 })
}
})
jiedaores.data.map((item, index) => {
if (item == data.jiedao) {
 this.setData({
jiedaoIndex: index,
jiedao: item
 })
}else if (this.data.jiedaoList.length == 1) {
this.setData({
jiedaoIndex: 0,
jiedao: v
 })
}
})
for (var o in data){
if(o=='xuehao') {
 if(!this.data.xuehao){
  dataEmpty["xuehao"] = data[o]
 }
}
if(o=='mima') {
 if(!this.data.mima){
  dataEmpty["mima"] = data[o]
 }
}
if(o=='xueshengxingming') {
 if(!this.data.xueshengxingming){
  dataEmpty["xueshengxingming"] = data[o]
 }
}
if(o=='xingbie') {
 if(!this.data.xingbie){
  dataEmpty["xingbie"] = data[o]
 }
}
if(o=='xueyuan') {
 if(!this.data.xueyuan){
  dataEmpty["xueyuan"] = data[o]
 }
}
if(o=='zhuanye') {
 if(!this.data.zhuanye){
  dataEmpty["zhuanye"] = data[o]
 }
}
if(o=='nianji') {
 if(!this.data.nianji){
  dataEmpty["nianji"] = data[o]
 }
}
if(o=='xueli') {
 if(!this.data.xueli){
  dataEmpty["xueli"] = data[o]
 }
}
if(o=='shifoubiye') {
 if(!this.data.shifoubiye){
  dataEmpty["shifoubiye"] = data[o]
 }
}
if(o=='biyenianfen') {
 if(!this.data.biyenianfen){
  dataEmpty["biyenianfen"] = data[o]
 }
}
if(o=='xiangmufuzeren') {
 if(!this.data.xiangmufuzeren){
  dataEmpty["xiangmufuzeren"] = data[o]
 }
}
if(o=='suozaixuexiao') {
 if(!this.data.suozaixuexiao){
  dataEmpty["suozaixuexiao"] = data[o]
 }
}
if(o=='youxiang') {
 if(!this.data.youxiang){
  dataEmpty["youxiang"] = data[o]
 }
}
if(o=='touxiang') {
dataEmpty['touxiang'] = data[o];
dataEmpty['touxiangPath'] = data[o].split(",")[0];
}
if(o=='chuangyelingyu') {
 if(!this.data.chuangyelingyu){
  dataEmpty["chuangyelingyu"] = data[o]
 }
}
if(o=='canjiahuodong') {
 if(!this.data.canjiahuodong){
  dataEmpty["canjiahuodong"] = data[o]
 }
}
if(o=='canyuxiangmu') {
 if(!this.data.canyuxiangmu){
  dataEmpty["canyuxiangmu"] = data[o]
 }
}
if(o=='leixing') {
 if(!this.data.leixing){
  dataEmpty["leixing"] = data[o]
 }
}
if(o=='chengshi') {
 if(!this.data.chengshi){
  dataEmpty["chengshi"] = data[o]
 }
}
if(o=='yuanqu') {
 if(!this.data.yuanqu){
  dataEmpty["yuanqu"] = data[o]
 }
}
if(o=='jiedao') {
 if(!this.data.jiedao){
  dataEmpty["jiedao"] = data[o]
 }
}
if(o=='email') {
 if(!this.data.email){
  dataEmpty["email"] = data[o]
 }
}
}
const url = wx.getStorageSync("baseURL") + "/"
const detailList = data
let  objtemp= {
detailList,
 touxiang:data?.touxiang?.split(',')[0],
 touxiangPath:data?.touxiang?.split(',')[0],
}
this.setData(objtemp);

this.setData(dataEmpty)
}else {
this.setData({
})
}
// ss读取
let sessionReadArr=[]
this.setData({
cross:options?.cross,
ro,
id,
})

if (wx.getStorageSync('raffleType') ) {
 wx.removeStorageSync('raffleType')
 setTimeout(() => {
  this.submit()
 }, 300)
}






















},
getUUID () {
return new Date().getTime();
},
onUnload: function () {
},
onShow() {
},
 commentScore(e){
 if(this.data?.commentScore){
  this.setData({
   commentScore:e.detail
  })
 }
 },






//xingbie, 0
// 下拉变化
xingbieChange(e) {
this.setData({
 xingbieIndex:   e.detail.value,
 xingbie :this.data.xingbieList[e.detail.value]
})
},

//xueli, 0
// 下拉变化
xueliChange(e) {
this.setData({
 xueliIndex:   e.detail.value,
 xueli :this.data.xueliList[e.detail.value]
})
},

//shifoubiye, 0
// 下拉变化
shifoubiyeChange(e) {
this.setData({
 shifoubiyeIndex:   e.detail.value,
 shifoubiye :this.data.shifoubiyeList[e.detail.value]
})
},

//xiangmufuzeren, 0
// 下拉变化
xiangmufuzerenChange(e) {
this.setData({
 xiangmufuzerenIndex:   e.detail.value,
 xiangmufuzeren :this.data.xiangmufuzerenList[e.detail.value]
})
},

//leixing, 0
// 下拉变化
leixingChange(e) {
this.setData({
 leixingIndex:   e.detail.value,
 leixing :this.data.leixingList[e.detail.value]
})
},

//chengshi, 1
//yuanqu, 2
async chengshiChange(e) {
this.setData({
 chengshiIndex:  e.detail.value*1,
 chengshi:this.data.chengshiList[e.detail.value*1]
})

let res = await levelOption(`option/yuanqu/yuanqu`, "chengshi",this.data.chengshiList[chengshiIndex])
if (res && res.code === 0) {
this.setData({
  chengshiList
})}
},
//jiedao, 3
async yuanquChange(e) {
this.setData({
 yuanquIndex:  e.detail.value*1,
 yuanqu:this.data.yuanquList[e.detail.value*1]
})

let res = await levelOption(`option/yuanqu/yuanqu`, "yuanqu",this.data.yuanquList[yuanquIndex])
if (res && res.code === 0) {
this.setData({
  yuanquList
})}
},
// 下拉变化
jiedaoChange(e) {
this.setData({
 jiedaoIndex:  e.detail.value,
 jiedao :this.data.jiedaoList[e.detail.value]
})
},





















































uploadtouxiang() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePaths = res.tempFilePaths;
// 本地临时图片的路径
this.setData({
 touxiangPath: tempFilePaths[0]
})
let _this = this;
// 上传网络图片
const baseURL = wx.getStorageSync('baseURL')
wx.uploadFile({
url: `${baseURL}/file/upload`,
filePath: res.tempFilePaths[0],
name: 'file',
header: {
 'Token': wx.getStorageSync('token')
},
success: (uploadFileRes) => {
 let result = JSON.parse(uploadFileRes.data);
// result.file是上传成功为网络图片的名称
 if (result.code == 0) {
  this.setData({
    touxiang: 'file/' + result.file
  })
 } else {
  wx.showToast({
   title: result.msg,
   icon: 'none',
   duration: 2000
  });
 }
}
})

this.setData({
face: tempFilePaths[0]
});

}
})
},






































async submit() {
let that = this
var query = wx.createSelectorQuery();


const baseURL = wx.getStorageSync('baseURL') + "/"
const regex = new RegExp(baseURL, "g");
const obj={
xuehao: this.data.xuehao,
xueshengxingming: this.data.xueshengxingming,
xingbie:this.data.xingbieList[this.data.xingbieIndex]?this.data.xingbieList[this.data.xingbieIndex]:'',
xueyuan: this.data.xueyuan,
zhuanye: this.data.zhuanye,
nianji: this.data.nianji,
xueli:this.data.xueliList[this.data.xueliIndex]?this.data.xueliList[this.data.xueliIndex]:'',
shifoubiye:this.data.shifoubiyeList[this.data.shifoubiyeIndex]?this.data.shifoubiyeList[this.data.shifoubiyeIndex]:'',
biyenianfen: this.data.biyenianfen,
xiangmufuzeren:this.data.xiangmufuzerenList[this.data.xiangmufuzerenIndex]?this.data.xiangmufuzerenList[this.data.xiangmufuzerenIndex]:'',
suozaixuexiao: this.data.suozaixuexiao,
youxiang: this.data.youxiang,
touxiang:this.data.touxiang?.replace(regex, ""),
chuangyelingyu: this.data.chuangyelingyu,
canjiahuodong: this.data.canjiahuodong,
canyuxiangmu: this.data.canyuxiangmu,
leixing:this.data.leixingList[this.data.leixingIndex]?this.data.leixingList[this.data.leixingIndex]:'',
chengshi:this.data.chengshiList[this.data.chengshiIndex]?this.data.chengshiList[this.data.chengshiIndex]:'',
yuanqu:this.data.yuanquList[this.data.yuanquIndex]?this.data.yuanquList[this.data.yuanquIndex]:'',
jiedao:this.data.jiedaoList[this.data.jiedaoIndex]?this.data.jiedaoList[this.data.jiedaoIndex]:'',
email: this.data.email,
}
const detailId= getApp().globalData.detailId
const tableName= `chuangyezhe`
//跨表计算判断
var obj2;
var  ruleForm=obj
obj2 = ruleForm
this.data.refid==""? ruleForm['refid']= getApp().globalData.detailId:""
ruleForm['userid']=getApp().globalData.userInfo.id;
var userInfo=getApp().globalData.userInfo
const sessionReadArr=this.data.sessionReadArr
const phonePattern = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;
const mobilePattern = /^(?:\+?86)?1[3-9]\d{9}$/;
const emailPattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
const idPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dxX]$/;
const urlPattern = /^(http|https):\/\/[\w\-]+(\.[\w\-]+)+[/#?]?.*$/;
 if(!this.data.xuehao ){
  wx.showToast({
   icon: "none",
   title: `学号不能为空`,
  })
  return
 }





 if(!this.data.mima ){
  wx.showToast({
   icon: "none",
   title: `密码不能为空`,
  })
  return
 }





 if(!this.data.xueshengxingming ){
  wx.showToast({
   icon: "none",
   title: `学生姓名不能为空`,
  })
  return
 }





 if(!this.data.xingbie ){
  wx.showToast({
   icon: "none",
   title: `性别不能为空`,
  })
  return
 }





 if(!this.data.xueyuan ){
  wx.showToast({
   icon: "none",
   title: `学院不能为空`,
  })
  return
 }





 if(!this.data.zhuanye ){
  wx.showToast({
   icon: "none",
   title: `专业不能为空`,
  })
  return
 }





 if(!this.data.nianji ){
  wx.showToast({
   icon: "none",
   title: `年级不能为空`,
  })
  return
 }





 if(!this.data.xueli ){
  wx.showToast({
   icon: "none",
   title: `学历不能为空`,
  })
  return
 }





 if(!this.data.shifoubiye ){
  wx.showToast({
   icon: "none",
   title: `是否毕业不能为空`,
  })
  return
 }





 if(!this.data.biyenianfen ){
  wx.showToast({
   icon: "none",
   title: `毕业年份不能为空`,
  })
  return
 }





 if(!this.data.xiangmufuzeren ){
  wx.showToast({
   icon: "none",
   title: `项目负责人不能为空`,
  })
  return
 }





 if(!this.data.suozaixuexiao ){
  wx.showToast({
   icon: "none",
   title: `所在学校不能为空`,
  })
  return
 }





 if(!this.data.youxiang ){
  wx.showToast({
   icon: "none",
   title: `邮箱不能为空`,
  })
  return
 }


const valueyouxiang = this.data.youxiang;

if (!emailPattern.test(valueyouxiang)) {
wx.showToast({
icon: "none",
title: `邮箱应输入邮件格式`,
})
return
}




































const valueemail = this.data.email;

if (!emailPattern.test(valueemail)) {
wx.showToast({
icon: "none",
title: `邮箱应输入邮件格式`,
})
return
}



//更新跨表属性
var crossuserid;
var crossrefid;
var crossoptnum;
if(this.data.cross) {
wx.setStorageSync('crossCleanType', true);
var statusColumnName = wx.getStorageSync('statusColumnName');
var statusColumnValue = wx.getStorageSync('statusColumnValue');
if (statusColumnName != '') {
obj2 = wx.getStorageSync('crossObj');
if (!statusColumnName.startsWith("[")) {
for (var o in obj2) {
if (statusColumnName==o){
obj2[o] = statusColumnValue;
}

}
var table = wx.getStorageSync('crossTable');

await update(table, obj2)
} else {

crossuserid =getApp().globalData.userInfo.id
crossrefid =  this.data.id
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.replace(/\[/, "").replace(/\]/, "");
}
}
}
 ruleForm?.shhf?ruleForm.shhf=this.data.shhf:''
if(crossrefid && crossuserid) {
ruleForm['crossuserid'] =getApp().globalData.userInfo.id;
ruleForm['crossrefid'] =obj2.id;
this.setData({
ruleForm
})
let params = {
page: 1,
limit: 10,
crossuserid: crossuserid,
crossrefid: crossrefid,
}
const tips = wx.getStorageSync('tips')
let corssRes = await list(`chuangyezhe`, params)
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.match(/\d+/g);
if (corssRes?.data?.total >= parseInt(crossoptnum)) {
wx.showToast({
icon: "none",
title: tips,
})
wx.removeStorageSync('crossCleanType');
return ;
}
else {
//跨表计算
if (ruleForm.id ) {
await update(`chuangyezhe`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
 await add(`chuangyezhe`, ruleForm)
}
}
}
else {
//跨表计算
if (ruleForm.id || this.data.editStatus) {
this.data.editStatus?ruleForm['id']= getApp().globalData.detailId:""
await update(`chuangyezhe`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
await add(`chuangyezhe`, ruleForm)
}
}
getApp().globalData.editorContent=''
wx.showToast({
title: '提交成功',
icon: "none"
})
const preId = getApp().globalData.detailId

if(table){
let res = await detail(table, preId)
if(res.code==0){
getApp().globalData.detailList = res.data
}

}
wx.navigateBack({
delta: 1,
complete: () => {
// 触发事件通知，传递需要更新的数据
const pages = getCurrentPages();
if (pages.length >= 1) {
const prePage = pages[pages.length - 1];
prePage.onLoad(); //
}
}
})

},
onHide() {

},


/**
* 生命周期函数--监听页面卸载
*/
onUnload() {

},


/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage() {

}
})